package com.loong.musicdemo.widget.CustomDialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.loong.musicdemo.R;

/**
 * 自定义标准对话框
 */

public class CommonDialog extends AlertDialog {//继承AlertDialog
    private TextView textViewTitle;//标题
    private TextView textViewMessage;//提示信息
    private Button buttonNegative;//取消按钮
    private Button buttonPositive;//确认按钮

    public CommonDialog(@NonNull Context context) {
        super(context);
    }
    private String title;
    private String message;
    private String negative;
    private String positive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //绑定视图文件
        setContentView(R.layout.widget_custom_dialog);
        initView();//初始化控件
        initEvent();//初始化事件
    }

    /**
     * 吃实话页面控件
     */
    private void initView() {
        textViewTitle = findViewById(R.id.title);
        textViewMessage = findViewById(R.id.message);
        buttonNegative = findViewById(R.id.negtive);
        buttonPositive = findViewById(R.id.positive);
    }

    /**
     * 初始化控件绑定的事件
     */
    private void initEvent() {
        //设置确认以及取消事件
        buttonPositive.setOnClickListener(v->{
            if (onclickBottomListener!=null){
                //绑定不为空时
                onclickBottomListener.onPositiveClick();
            }
        });
        //设置取消按钮
        buttonNegative.setOnClickListener(v->{
            if (onclickBottomListener!=null)
                onclickBottomListener.onNegativeClick();
        });
    }
    //内部接口实现按钮监听
    public interface OnclickBottomListener{
        //实现确认按钮
        void onPositiveClick();
        //实现取消按钮
        void onNegativeClick();
    }
    private OnclickBottomListener onclickBottomListener;

    /**
     * 按钮监听方法
     * @param onclickBottomListener 绑定按钮接听的接口
     * @return 当亲对话框对象
     */
    public CommonDialog setOnclickBottomListener(OnclickBottomListener onclickBottomListener){
        this.onclickBottomListener = onclickBottomListener;//绑定监听事件
        return this;
    }

    /**
     * 设置标题
     * @param title 对话框标题
     * @return 当前对话框对象
     */
    public CommonDialog setTitle(String title){
        this.title = title;
        return this;
    }

    /**
     * 设置提示信息
     * @param message 提示信息
     * @return 当前对话框对象
     */
    public CommonDialog setMessage(String message){
        this.message = message;
        return this;
    }
    public CommonDialog setPositive(String positive){
        this.positive = positive;
        return this;
    }
    public CommonDialog setNegative(String negative){
        this.negative = negative;
        return this;
    }

    @Override
    public void show() {
        super.show();
        refreshView();//显示控件数据
    }

    private void refreshView() {
        //如果提供了选项，则显示对应数据，否则显示默认
        if (!TextUtils.isEmpty(title)){
            textViewTitle.setText(title);
            textViewTitle.setVisibility(View.VISIBLE);
        }else
            textViewTitle.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(message))
            textViewMessage.setText(message);

        if (!TextUtils.isEmpty(positive))
            buttonPositive.setText(positive);
        else buttonPositive.setText("确认");

        if (!TextUtils.isEmpty(negative))
            buttonNegative.setText(negative);
        else buttonNegative.setText("取消");
    }
}
